package com.clean.scanlibrary.dialog

import android.app.Dialog
import android.content.ClipboardManager
import android.content.Context
import android.content.DialogInterface
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.ScrollView
import android.widget.TextView
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import oa.lla.cca.R
import com.clean.scanlibrary.adapter.DiscernTipsAdapter
import com.clean.scanlibrary.bean.CurrencyInfoBean
import com.clean.scanlibrary.bean.DiscernInfoBean
import com.clean.scanlibrary.bean.WordInfoBean


class DiscernTipsDialog:Dialog {

    private var mContext:Context? = null
    private var dialog_discern_list:RecyclerView? = null
    private var copy_tv:TextView? = null
    private var words_scroll_view:NestedScrollView? = null
    private var words_tv:TextView? = null
    private var copyText = ""
    private var discern_img:ImageView? = null

    constructor(context: Context) : super(context){
        mContext = context
        initView()
    }
    constructor(context: Context, themeResId: Int) : super(context, themeResId){
        mContext = context
        initView()
    }
    constructor(
        context: Context,
        cancelable: Boolean,
        cancelListener: DialogInterface.OnCancelListener?
    ) : super(context, cancelable, cancelListener){
        mContext = context
        initView()
    }


    private fun initView(){
        val inflate =
            LayoutInflater.from(mContext).inflate(R.layout.discerntips_dialog_layout, null)
        setContentView(inflate)
        dialog_discern_list = inflate.findViewById(R.id.dialog_discern_list)
        copy_tv = inflate.findViewById(R.id.copy_tv)
        words_scroll_view = inflate.findViewById(R.id.words_scroll_view)
        words_tv = inflate.findViewById(R.id.words_tv)
        discern_img = inflate.findViewById(R.id.discern_img)


        //一键复制
        copy_tv?.setOnClickListener {
            val cbm =mContext?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            cbm.text = copyText
            Toast.makeText(mContext,"复制成功",Toast.LENGTH_SHORT).show()
        }


    }


    fun setWordsInfoData(wordData:WordInfoBean,path:String){
        dialog_discern_list?.visibility = View.GONE
        words_scroll_view?.visibility = View.VISIBLE
        copy_tv?.visibility = View.VISIBLE
        val sb:StringBuilder = StringBuilder()
        try {
            for (item in wordData.words_result){
                sb.append(item.words+"\n")
            }
            copyText = sb.toString()
            words_tv?.text = sb.toString()

            Glide.with(mContext!!).load(path).into(discern_img!!)
        } catch (e: Exception) {
        }
    }


     fun setInfoData(dataList:ArrayList<DiscernInfoBean>,path:String){
        dialog_discern_list?.visibility = View.VISIBLE
        words_scroll_view?.visibility = View.GONE
         copy_tv?.visibility = View.GONE
        dialog_discern_list?.layoutManager = LinearLayoutManager(context)
        dialog_discern_list?.adapter = DiscernTipsAdapter(context,dataList)
         Glide.with(mContext!!).load(path).into(discern_img!!)

    }

    fun setCurrencyData(currcyInfo: CurrencyInfoBean,path: String){
        dialog_discern_list?.visibility = View.GONE
        words_scroll_view?.visibility = View.VISIBLE
        copy_tv?.visibility = View.GONE
        words_tv?.visibility = View.VISIBLE

        if(TextUtils.isEmpty(currcyInfo.currencyName)){
            copyText = "识别失败"
        } else {
            copyText = currcyInfo.currencyName
        }

        words_tv?.text = copyText

        Glide.with(mContext!!).load(path).into(discern_img!!)
    }
}